<template>
  <el-card header="流程事件列表">
    <div class="app-container">
      <el-form :inline="true" :model="queryParam">
        <el-form-item label="事件名">
          <el-input
            v-model="queryParam.QueryKey"
            placeholder="事件名"
            style="width: 200px"
            @change="search"
          />
        </el-form-item>
        <el-form-item label="使用范围">
          <enumItem
            v-model="queryParam.UseRange"
            :dic-key="EnumDic.FlowUseRange"
            placeholder="使用范围"
            :multiple="true"
            :enum-items.sync="useRange"
            sys-head="flow"
            @change="search"
          />
        </el-form-item>
        <el-form-item label="接口状态">
          <enumItem
            v-model="queryParam.Status"
            :dic-key="EnumDic.FlowInterStatus"
            placeholder="接口状态"
            :multiple="true"
            sys-head="flow"
            @change="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="default"
            @click="reset()"
          >重置</el-button>
          <el-button
            type="success"
            @click="add()"
          >新增事件</el-button>
        </el-form-item>
      </el-form>
    </div>
    <w-table
      :data="flowEvents"
      :columns="columns"
      :is-paging="true"
      row-key="Id"
      :paging="paging"
      @load="load"
    >
      <template slot="createTime" slot-scope="e">
        {{ moment(e.row.CreateTime).format('YYYY-MM-DD HH:mm') }}
      </template>
      <template slot="interType" slot-scope="e">
        {{ FlowInterType[e.row.InterType].text }}
      </template>
      <template slot="useRange" slot-scope="e">
        {{ getTextName(e.row.UseRange, useRange) }}
      </template>
      <template slot="status" slot-scope="e">
        <el-switch
          :value="e.row.Status == 1"
          :inactive-text="e.row.Status == 0 ? FlowInterStatus[0].text : FlowInterStatus[2].text"
          active-text="启用"
          @change="(val) => setStatus(e.row, val)"
        />
      </template>
      <template slot="action" slot-scope="e">
        <el-button
          v-if="e.row.Status === 1"
          icon="el-icon-view"
          size="mini"
          type="primary"
          circle
          @click="show(e.row)"
        />
        <el-button
          v-if="e.row.Status !== 1"
          icon="el-icon-edit"
          size="mini"
          type="primary"
          circle
          @click="edit(e.row)"
        />
        <el-button
          v-if="e.row.Status === 0"
          size="mini"
          icon="el-icon-delete"
          type="danger"
          circle
          @click="deleteFlow(e.row)"
        />
      </template>
    </w-table>
    <flowInterEdit :id="id" :visible.sync="visible" @close="closeEdit" />
    <flowInterView :id="id" :visible.sync="viewVisible" />
  </el-card>
</template>

<script>
import moment from 'moment'
import { EnumDic, FlowInterType, FlowInterStatus } from '@/flow/config/flowConfig'
import * as interApi from '@/flow/api/flowInter'
import flowInterEdit from './components/flowInterEdit.vue'
import flowInterView from './components/flowInterView.vue'
export default {
  components: {
    flowInterEdit,
    flowInterView
  },
  data() {
    return {
      EnumDic,
      FlowInterType,
      FlowInterStatus,
      flowEvents: [],
      id: null,
      visible: false,
      viewVisible: false,
      useRange: [],
      queryParam: {
        Status: [],
        QueryKey: null,
        InterType: null,
        UseRange: null
      },
      columns: [
        {
          key: 'InterName',
          title: '事件名',
          align: 'left',
          minWidth: 250
        },
        {
          sortby: 'InterType',
          key: 'InterType',
          title: '通信类型',
          align: 'center',
          width: 150,
          slotName: 'interType',
          sortable: 'custom'
        },
        {
          sortby: 'UseRange',
          key: 'UseRange',
          title: '事件类型',
          align: 'center',
          width: 150,
          slotName: 'useRange',
          sortable: 'custom'
        },
        {
          sortby: 'Status',
          key: 'Status',
          title: '状态',
          align: 'center',
          width: 150,
          slotName: 'status',
          sortable: 'custom'
        },
        {
          key: 'CreateTime',
          title: '创建时间',
          align: 'center',
          slotName: 'createTime',
          sortable: 'custom',
          width: 150
        },
        {
          key: 'Action',
          title: '操作',
          align: 'left',
          width: 200,
          slotName: 'action'
        }
      ],
      paging: {
        Size: 20,
        Index: 1,
        SortName: 'Id',
        IsDesc: false,
        Total: 0
      }
    }
  },
  computed: {},
  mounted() {
    this.reset()
  },
  methods: {
    moment,
    getTextName(val, list) {
      const item = list.find(a => a.Value === val)
      if (item) {
        return item.Text
      }
      return null
    },
    closeEdit(isRefresh) {
      if (isRefresh) {
        this.load()
      }
    },
    add() {
      this.id = null
      this.visible = true
    },
    edit(row) {
      this.id = row.Id
      this.visible = true
    },
    show(row) {
      this.id = row.Id
      this.viewVisible = true
    },
    reset() {
      this.queryParam.QueryKey = null
      this.queryParam.Status = null
      this.queryParam.UseRange = null
      this.queryParam.InterType = null
      this.paging.Index = 1
      this.load()
    },
    search() {
      this.paging.Index = 1
      this.load()
    },
    async load() {
      const res = await interApi.Query(this.queryParam, this.paging)
      if (res.List) {
        this.flowEvents = res.List
      } else {
        this.flowEvents = []
      }
      this.paging.Total = res.Count
    },
    async setStatus(row, value) {
      if (value) {
        await interApi.Enable(row.Id)
      } else {
        await interApi.Disable(row.Id)
      }
      // eslint-disable-next-line require-atomic-updates
      row.Status = value ? 1 : 2
    },
    deleteFlow(row) {
      const title =
        '确认删除事件:' + row.InterName + '?'
      const that = this
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.submitDrop(row.Id)
      })
    },
    async submitDrop(id) {
      await interApi.Delete(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.reset()
    }
  }
}
</script>
<style scoped>
.rowSpan {
    display: inline-block;
    width: 100%;
}
</style>
